package ActionListeners;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JTextField;

public class MascaraTipoAlfanumericoListener implements KeyListener  {
	public void keyPressed(KeyEvent e) {
			
	}

	@Override
	public void keyReleased(KeyEvent e) {

		}

	@Override
	public void keyTyped(KeyEvent e) {
		 char caracter = e.getKeyChar();
		 String texto = ((JTextField) e.getSource()).getText();
		 int cantidadCaracteres = ((JTextField) e.getSource()).getText().length();
	     // Verificar si la tecla pulsada no es un digito
	     if(!(Character.isLetterOrDigit(caracter) || caracter==' ') || (caracter ==' ' && cantidadCaracteres==0)
	    		 || (caracter==' ' && cantidadCaracteres>0 && texto.charAt(cantidadCaracteres-1)==' '))
			 e.consume();
	}
}